package com.cooksys.film_rental.bo.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cooksys.film_rental.bo.FilmCategoryBo;
import com.cooksys.film_rental.dao.FilmCategoryDao;
import com.cooksys.film_rental.model.Category;
import com.cooksys.film_rental.model.Film;
import com.cooksys.film_rental.model.FilmCategory;
import com.cooksys.film_rental.model.FilmCategoryId;

@Service("FilmcategoryBo")
public class FilmCategoryBoImpl implements FilmCategoryBo {

	@Autowired(required = true)
	FilmCategoryDao filmCategoryDao;

	@Override
	public FilmCategory getFilmCategoryByFilmCategoryId(Short filmId,
			Byte categoryId) {
		FilmCategoryId filmCategoryId = new FilmCategoryId();
		filmCategoryId.setCategoryId(categoryId);
		filmCategoryId.setFilmId(filmId);
		return this.getFilmCategoryByFilmCategoryId(filmCategoryId);
	}

	@Override
	public FilmCategory getFilmCategoryByFilmCategoryId(String filmId,
			Byte categoryId) {
		try {

			Short filmIdAsShort = Short.valueOf(filmId);
			return this.getFilmCategoryByFilmCategoryId(filmIdAsShort,
					categoryId);

		} catch (NumberFormatException e) {

			System.err
					.println("Could not parse Short value for filmId from string: "
							+ filmId);
			e.printStackTrace();
			System.err.println("Returning null value...");
			return null;

		}
	}

	@Override
	public FilmCategory getFilmCategoryByFilmCategoryId(Short filmId,
			String categoryId) {
		try {

			Byte categoryIdAsByte = Byte.valueOf(categoryId);
			return this.getFilmCategoryByFilmCategoryId(filmId,
					categoryIdAsByte);

		} catch (NumberFormatException e) {

			System.err
					.println("Could not parse Byte value for categoryId from string: "
							+ filmId);
			e.printStackTrace();
			System.err.println("Returning null value...");
			return null;

		}
	}

	@Override
	public FilmCategory getFilmCategoryByFilmCategoryId(String filmId,
			String categoryId) {
		try {

			Short filmIdAsShort = Short.valueOf(filmId);
			Byte categoryIdAsByte = Byte.valueOf(categoryId);
			return this.getFilmCategoryByFilmCategoryId(filmIdAsShort,
					categoryIdAsByte);

		} catch (NumberFormatException e) {

			System.err
					.println("Could not parse Short value for filmId from string: "
							+ filmId
							+ " or Byte value for categoryId from string: "
							+ categoryId);
			e.printStackTrace();
			System.err.println("Returning null value...");
			return null;

		}
	}

	@Override
	public FilmCategory getFilmCategoryByFilmCategoryId(
			FilmCategoryId filmCategoryId) {
		return this.filmCategoryDao
				.getFilmCategoryByFilmCategoryId(filmCategoryId);
	}

	@Override
	public List<FilmCategory> searchFilmCategoriesByFilm(Film film) {
		return this.filmCategoryDao.searchFilmCategoriesByFilm(film);
	}

	@Override
	public List<FilmCategory> searchFilmCategoriesByCategory(Category category) {
		return this.filmCategoryDao.searchFilmCategoriesByCategory(category);
	}

}
